package crap;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

import org.ejml.simple.SimpleMatrix;

public class CTester03 {
	
	public static void main(String[] args) throws Exception{
		final BufferedImage bi = ImageIO.read(CTester02.class.getClassLoader().getResourceAsStream("res/P5100207s.JPG"));
		final double mw = 320, mh = 240;
		final Point2D[] camPoints = {new Point2D.Double(400-mw,160-mh), new Point2D.Double(464-mw,344-mh), new Point2D.Double(167-mw,393-mh), new Point2D.Double(119-mw, 191-mh)};
		final Point2D[] A4Points = {new Point2D.Double(0, 0), new Point2D.Double(0.210, 0), new Point2D.Double(0.210, 0.297), new Point2D.Double(0, 0.297)};
		
		// matrix == { {row1}, {row2} }
		
		double x = 5, y = 6, z = 3;
		double[][] dataX = {{x},{y},{z}};
		SimpleMatrix X = new SimpleMatrix(dataX);
		System.out.println(X);
		
		double[][] dataF1 = { {1/z,0,0},{0,1/z,0},{0,0,1/z} };
		SimpleMatrix F1 = new SimpleMatrix( dataF1 );
		System.out.println(F1);
		
		SimpleMatrix X_ = F1.mult(X);
		System.out.println(X_);
		
		
		
	}
	
	
//	public static void main(String[] args) throws Exception {
//		final BufferedImage bi = ImageIO.read(CTester02.class.getClassLoader().getResourceAsStream("res/P5100207s.JPG"));
//		final double mw = 320, mh = 240;
//		final Point2D[] camPoints = {new Point2D.Double(400-mw,160-mh), new Point2D.Double(464-mw,344-mh), new Point2D.Double(167-mw,393-mh), new Point2D.Double(119-mw, 191-mh)};
//		final Point2D[] A4Points = {new Point2D.Double(0, 0), new Point2D.Double(0.210, 0), new Point2D.Double(0.210, 0.297), new Point2D.Double(0, 0.297)};
//		final Path2D markerLine = new Path2D.Double();
//
//		markerLine.moveTo(camPoints[0].getX()+mw, camPoints[0].getY()+mh);
//		markerLine.lineTo(camPoints[1].getX()+mw, camPoints[1].getY()+mh);
//		markerLine.lineTo(camPoints[2].getX()+mw, camPoints[2].getY()+mh);
//		markerLine.lineTo(camPoints[3].getX()+mw, camPoints[3].getY()+mh);
//		markerLine.closePath();
//		
//		JFrame ff = new JFrame();
//		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		
//		final JComponent canvas = new JComponent() {
//			{setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));}
//			@Override
//			public void paint(Graphics g) {
//				Graphics2D g2 = (Graphics2D)g;
//				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//				g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
//				g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//				
//				g2.drawImage(bi, 0, 0, this);
//				g2.setStroke(new BasicStroke(2f));
//				g2.setColor(Color.red);
//				g2.draw(markerLine);
//				
//				super.paint(g);
//			}
//		};
//		
////		MouseListener ml = new MouseAdapter() {
////			@Override
////			public void mouseClicked(MouseEvent e) {
////				System.out.println(e.getPoint());
////				super.mouseClicked(e);
////			}
////		};
////		canvas.addMouseListener(ml);
//		
//		SimpleMatrix leftSide = new SimpleMatrix(8, 8), rightSide = new SimpleMatrix(8, 1);
//		
//		
//		for (int i = 0; i<4; i++) {
//			
//			double x = A4Points[i].getX(), y = A4Points[i].getY(), x_ = camPoints[i].getX(), y_ = camPoints[i].getY();
//			//x_ = (x y 1 0 0 0 -x_*x -x_*y ) h = x_
//			leftSide.set(i*2, 0, x);
//			leftSide.set(i*2, 1, y);
//			leftSide.set(i*2, 2, 1);
//
//			leftSide.set(i*2, 3, 0);
//			leftSide.set(i*2, 4, 0);
//			leftSide.set(i*2, 5, 0);
//			
//			leftSide.set(i*2, 6, -x_*x);
//			leftSide.set(i*2, 7, -x_*y);
//			
//			//y_ = (0 0 0 x y 1 -y_*x -y_*y) h = y_
//			leftSide.set(i*2+1, 0, 0);
//			leftSide.set(i*2+1, 1, 0);
//			leftSide.set(i*2+1, 2, 0);
//
//			leftSide.set(i*2+1, 3, x);
//			leftSide.set(i*2+1, 4, y);
//			leftSide.set(i*2+1, 5, 1);
//			
//			leftSide.set(i*2+1, 6, -y_*x);
//			leftSide.set(i*2+1, 7, -y_*y);
//			
//			// x_ und y_ im Ergebnisvektor setzen 
//			rightSide.set(i*2, 0, x_);
//			rightSide.set(i*2+1, 0, y_);
//		}
//		
//		System.out.println(leftSide);
//		System.out.println(rightSide);
//		
////		System.out.println(leftSide.svd().getW());
////		System.out.println(leftSide.invert().mult(rightSide));
//		SimpleMatrix h = leftSide.solve(rightSide);
//		
//		double h11 = h.get(0, 0), h12 = h.get(1,0), h13 = h.get(2, 0), h21 = h.get(3, 0), h22 = h.get(4, 0), h23 = h.get(5, 0), h31 = h.get(6, 0), h32 = h.get(7, 0);
//		double lambda1 = Math.sqrt(h11*h11+h21*h21+h31*h31), lambda2 = Math.sqrt(h12*h12+h22*h22+h32*h32);
//		
//		System.out.println("lambda1: "+lambda1+"   lambda2: "+lambda2);
//		
//		double lambda = Math.max(lambda1, lambda2);
//
//		h11/=lambda;
//		h12/=lambda;
//		h13/=lambda;
//		h21/=lambda;
//		h22/=lambda;
//		h23/=lambda;
//		h31/=lambda;
//		h32/=lambda;
//		
//		double scale = 0.038*640/0.0173;
//		
//		h11*=scale;
//		h12*=scale;
//		h13*=scale;
//		h21*=scale;
//		h22*=scale;
//		h23*=scale;
//		h31*=scale;
//		h32*=scale;
//		
//		double[][] matrix = {{h11, h12, h21*h32-h22*h31},{h21, h22, h31*h12-h32*h11},{h31, h32, h11*h22-h21*h12}};
//		double[][] kma = {{scale, 0, 0},{0, scale, 0},{0, 0, 1}};
//		
//		SimpleMatrix K = new SimpleMatrix(kma);
//		
//		System.out.println("h11: "+h11+" h12: "+h12+" h13: "+h13+" h21: "+h21+" h22: "+h22+" h23: "+h23+" h31: "+h31+" h32: "+h32);
//		
//		SimpleMatrix result = new SimpleMatrix(4,4), R = new SimpleMatrix(matrix), t = new SimpleMatrix(3,1);
//		
//		t.set(0, 0, h13);
//		t.set(1, 0, h23);
//		t.set(2, 0, 1);
//		
//		System.out.println();
//		System.out.println(R);
//		System.out.println();
//		System.out.println(t);
//		System.out.println(R.invert().mult(t.scale(-1)));
////		System.out.println(h);
//		
//		ff.add(canvas);
//		
//		ff.pack();
//		ff.setVisible(true);
//		
//	}
}
